package com.test.clyq.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.dao.DataAccessException;

import com.test.clyq.dao.ContentDao;
import com.test.clyq.entity.Content;
import com.test.clyq.entity.Users;
import com.test.dao.hibernate.BaseDaoHibernate;
import com.test.util.FormatExceptionStack;
import com.test.util.Pagination;
import com.test.util.SessionFactoryUtil;

/**
 * ContentDaoHibernate. <br>
 * 评价选项内容管理.
 * <p>
 * Copyright: Copyright (c) 2010-3-17 下午04:30:46
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@SuppressWarnings("unchecked")
public class ContentDaoHibernate extends BaseDaoHibernate implements ContentDao {

	/**
	 * 添加评价选项内容
	 * 
	 * @param file
	 * @param user
	 */
	public void addContent(Content file, Users user) {
		this.log.info("新增评价选项内容start");
		try {
			saveOrUpdateItem(file, user);
			this.log.debug("新增评价选项内容成功");
			this.log.info("新增评价选项内容end");
		}
		catch (DataAccessException e) {
			this.log.error("[Content]addContent发生错误！\n" + FormatExceptionStack.formatException(e));
		}

	}

	/**
	 * 删除评价选项内容
	 * 
	 * @param id
	 * @param user
	 */
	public boolean deleteContent(Long id, Users user) {
		this.log.info("根据评价选项内容ID删除评价选项内容start");
		try {

			if (user != null) {
				removeItem(Content.class, id, user);
				this.log.debug("删除评价选项内容成功");
				this.log.info("根据评价选项内容ID删除评价选项内容end");
				return true;
			} else {
				this.log.debug("删除评价选项内容失败");
				return false;
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Content]deleteContent发生错误！\n" + FormatExceptionStack.formatException(e));
			return false;
		}
	}

	/**
	 * 通过分页获取评价选项内容
	 * 
	 * @param pagination
	 * @return
	 */
	public List<Content> getContentByPage(Pagination pagination, Long categoryId) {
		this.log.info("获得评价选项内容列表start");
		String sql = "from Content as file where file.contentQuestionId = " + categoryId + " order by file.date desc";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("select count(*) " + sql);
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(sql.toString());
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得评价选项内容列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<Content>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Content]getContentByPage发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<Content>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}

	}

	/**
	 * 获取所有评价选项内容
	 * 
	 * @return
	 */
	public List<Content> getContents(Long categoryId) {
		this.log.info("获得现在文件列表start");
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("from Content as file where category.categoryId = " + categoryId
			        + " order by file.isTip desc, file.weight desc, file.uploadTime desc");
			List list = query.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得评价选项内容列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<Content>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Content]getContents发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<Content>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}

	}

	/**
	 * 通过父栏目id获取其下所有子栏目的文件列表
	 * 
	 * @param categoryId
	 * @param pagination
	 * @param where
	 * @param order
	 * @return
	 */
	public List<Content> getContentsByParentCategoryId(Long categoryId, Pagination pagination) {
		this.log.info("根据父栏目id获取其下所有子栏目的文件列表start");
		String sql = " from Content as file where (file.isTip=1 or file.wapFirstPage=1) and file.category.id in ( select category.id from Category category where category.categoryParentId=:parentId or category.id=:PcategoryId) order by file.wapFirstPage desc";

		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("select count(*)" + sql);
			query.setLong("parentId", categoryId);
			query.setLong("PcategoryId", categoryId);
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(sql.toString());
			query1.setLong("parentId", categoryId);
			query1.setLong("PcategoryId", categoryId);
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("根据父栏目id获取其下所有子栏目的文件列表成功");
				this.log.info("根据父栏目id获取其下所有子栏目的文件列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<Content>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Content]getContentsByParentCategoryId！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<Content>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 获取一个评价选项内容
	 */
	public Content getOneContent(Long id) {
		this.log.info("根据评价选项内容ID获得评价选项内容start");
		try {
			Content file = (Content) getItem(Content.class, id);
			if (file != null) {
				this.log.info("根据评价选项内容ID获得评价选项内容end");
				return file;
			} else {
				this.log.info("根据评价选项内容ID获得评价选项内容end");
				return new Content();
			}

		}
		catch (DataAccessException e) {
			this.log.error("[Content]getOneContent发生错误！\n" + FormatExceptionStack.formatException(e));
			return new Content();
		}
	}

	/**
	 * 修改评价选项内容
	 * 
	 * @param id
	 * @param user
	 */
	public void updateContent(Content file, Users user) {
		this.log.info("保存或更新评价选项内容start");
		try {
			saveOrUpdateItem(file, user);
			this.log.debug("保存或更新评价选项内容成功");
			this.log.info("保存或更新评价选项内容end");
		}
		catch (DataAccessException e) {
			this.log.error("[Content]updateContent发生错误！\n" + FormatExceptionStack.formatException(e));
		}
	}
}
